package com.run.web.backend;
import com.github.pagehelper.Page;
import com.run.model.EbUser;
import com.run.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        List<EbUser> ebUsers=userService.list(page,pageSize);
     model.addAttribute("ebUsers",ebUsers);
     model.addAttribute("curPage",page);
     model.addAttribute("maxPage",((Page)ebUsers).getPages());
     return "manage/user";
    }
    @RequestMapping("/to_save")
    public String toSave(){
        System.out.println("111111111");
        return "manage/user-add";
    }
    @RequestMapping("/save")
    public String save(EbUser ebUser){
        System.out.println(ebUser);
        Integer result = userService.save(ebUser);
        if(result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/to_edit")
    public String toEdit(Model model,
                         @RequestParam("euUserId") String euUserId) {
        System.out.println("点击修改跳过来了,euUserId="+euUserId);
        EbUser ebUser = userService.selectOne(euUserId);
        model.addAttribute("ebUser",ebUser);
        return "manage/user-modify";
    }
    @RequestMapping("/edit")
    public String edit(EbUser ebUser){
        System.out.println("ebUser==>"+ebUser);
        Integer result=userService.updateUser(ebUser);
        System.out.println(result);
        if(result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
//        return null;
    }
    @RequestMapping("/delet")
  public String delet(Model model,
                        @RequestParam("euUserId")  String euUserId){

        int result = userService.deletUser(euUserId);

        model.addAttribute("euUserId",result);

        return "manage/manage-result";
    }

}
